package com.ohos.carbon.slice;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.DataBean;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;

import java.util.ArrayList;

import static com.ohos.carbon.slice.MainAbilitySlice.preferences;
public class FeaturesSlice extends AbilitySlice implements Component.ClickedListener {

    private Image back_button;
    private Text text_title, text;
    private ListContainer listContainer;
    private ArrayList<DataBean> mList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_features);
        initData();
        initView();
        initListener();
    }

    private void initListener() {
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        listContainer.setItemClickedListener((container, component, position, id) -> {
            DataBean item = (DataBean) listContainer.getItemProvider().getItem(position);
            Intent intent = new Intent();
            switch (position) {
                case 1:
                    present(new GesturedetectorSlice(), intent);
                    break;
                case 2:
                    present(new AutoSizeTextSlice(), intent);
                    break;
                case 3:
                    present(new BehaviorSlice(), intent);
                    break;
                case 4:
                    present(new HtmlSlice(), intent);
                    break;
                case 5:
                    present(new XmlFontSlice(), intent);
                    break;
                case 6:
                    present(new ShadowSlice(), intent);
                    break;
                case 7:
                    present(new PaginationSlice(), intent);
                    break;
                case 8:
                    present(new TextAppearanceSlice(), intent);
                    break;
                case 9:
                    present(new SVGiconsSlice(), intent);
                    break;
                case 10:
                    present(new ZOrderSlice(), intent);
                    break;
                case 11:
                    present(new RoundedSlice(), intent);
                    break;
                case 12:
                    present(new AnchorsSlice(), intent);
                    break;
                case 13:
                    present(new TextMarkerSlice(), intent);
                    break;
                default:
                    break;
            }
        });
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        back_button.setClickedListener(this);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Features");
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        listContainer.setItemProvider(new WhatsProvider(mList, getContext()));

        //返回上个页面
        back_button.setClickedListener(component -> {
            onBackPressed();
        });

    }

    private void initData() {
        mList = new ArrayList<>();
        Element favour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_border_black_24dp);
        Element starFavour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_gray_24dp);
        Element gesture = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_gesture_black_24dp);
        Element font = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_font_download_black_24dp);
        Element corners = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_rounded_corner_black_24dp);

        mList.add(new DataBean(null, "Carbon adds tons of useful features to all popular widgets.These include rounded comers,HTML text.elevation system,anchors and others", null, null));
        mList.add(new DataBean(gesture, "Gesture detectot", null, preferences.getBoolean("Gesture detectot",false)?starFavour:favour));
        mList.add(new DataBean(null, "Auto size text", null, preferences.getBoolean("Auto size text",false)?starFavour:favour));
        mList.add(new DataBean(null, "Behavior", null, preferences.getBoolean("Behavior",false)?starFavour:favour));
        mList.add(new DataBean(null, "HTML", null, preferences.getBoolean("HTML",false)?starFavour:favour));
        mList.add(new DataBean(font, "XML Font", null, preferences.getBoolean("XML Font",false)?starFavour:favour));
        mList.add(new DataBean(null, "Shadow", null, preferences.getBoolean("Shadow",false)?starFavour:favour));
        mList.add(new DataBean(null, "Pagination", null, preferences.getBoolean("Pagination",false)?starFavour:favour));
        mList.add(new DataBean(null, "Text appearance", null, preferences.getBoolean("Text appearance",false)?starFavour:favour));
        mList.add(new DataBean(null, "SVG icons", null, preferences.getBoolean("SVG icons",false)?starFavour:favour));
        mList.add(new DataBean(null, "Z order", null, preferences.getBoolean("Z order",false)?starFavour:favour));
        mList.add(new DataBean(corners, "Rounded corners", null, preferences.getBoolean("Rounded corners",false)?starFavour:favour));
        mList.add(new DataBean(null, "Anchors", null, preferences.getBoolean("Anchors",false)?starFavour:favour));
        mList.add(new DataBean(null, "Text marker", null, preferences.getBoolean("Text marker",false)?starFavour:favour));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        onBackPressed();
    }
}
